package com.ruoyi.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.config.AliConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.service.WebClientService;
import com.ruoyi.system.domain.ParkPriceRequest;
import com.ruoyi.system.service.ParkPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parking")
public class ParkingInformationController {

    @Autowired
    public ObjectMapper objectMapper;

    private final WebClient webClient;

    @Autowired
    public ParkPriceService parkPriceService;

    private final WebClientService webClientService;

    public ParkingInformationController(@Autowired WebClient webClient, WebClientService webClientService) {
        this.webClient = webClient;
        this.webClientService = webClientService;
    }

    @PostMapping("/getParkId")
    public AjaxResult getParkId(@RequestBody String qrcodeId) {
        String url = AliConfig.ParkingApi + "?qrcodeId="+qrcodeId;
        Mono<String> result = webClient.get().uri(url).retrieve().bodyToMono(String.class);
        String response = result.block();
        if (response.length()<=39)
            return AjaxResult.error("无此车场信息");
        try {
            Object json = objectMapper.readValue(response, Object.class);
            return AjaxResult.success("查询成功",json);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
            return AjaxResult.error();
        }
    }

    @PostMapping("/getParkPrcie")
    public AjaxResult getParkPrcie(@RequestBody ParkPriceRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parkid", request.getParkId());
        map.put("carnum",request.getCarNum());
        map.put("access_type","scan_center_pay");
        List<String> sign = AliConfig.sign(map, request.getSecret());
        String encodedPlateNumber;
        try {
        encodedPlateNumber = URLEncoder.encode(request.getCarNum().toUpperCase(), StandardCharsets.UTF_8.toString());
        String url = AliConfig.ParkingExpenses+"?"+sign.get(1)+"&carnum="+encodedPlateNumber+"&parkid="+request.getParkId()+"&access_type=scan_center_pay";
        Mono<String> mono = webClient.post()
                .uri(url)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .retrieve()
                .bodyToMono(String.class);
        String block = mono.block();
            Object value = objectMapper.readValue(block, Object.class);
            System.out.println(value+"");

            return AjaxResult.success("查询成功",value);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
